/*
 * Copyright 2012-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.web.servlet;

import javax.servlet.Filter;
import javax.servlet.ServletContext;

import org.springframework.util.Assert;

/**
 * 用于在 Servlet 3.0+ 容器中注册 {@link Filter} 的 {@link ServletContextInitializer} 实现。
 * 类似于 {@link ServletContext#addFilter(String, Filter)} 提供的注册功能，但设计上更适合 Spring Bean 使用。
 * <p>
 * 在调用 {@link #onStartup(ServletContext)} 之前，必须指定 {@link #setFilter(Filter)}。
 * 可以通过 {@link #setUrlPatterns URL 模式} 和/或 servlet（通过 {@link #setServletNames} 指定名称，
 * 或通过 {@link #setServletRegistrationBeans} 指定 {@link ServletRegistrationBean}）将注册与之关联。
 * 如果未指定 URL 模式或 servlet，则该过滤器会默认关联到 '/*'。
 * 过滤器名称如果未指定将自动推断。
 *
 * @param <T> 要注册的 {@link Filter} 类型
 * @author Phillip Webb
 * @since 1.4.0
 * @see ServletContextInitializer
 * @see ServletContext#addFilter(String, Filter)
 * @see DelegatingFilterProxyRegistrationBean
 */
public class FilterRegistrationBean<T extends Filter> extends AbstractFilterRegistrationBean<T> {

	private T filter;

	/**
	 * 创建一个新的 {@link FilterRegistrationBean} 实例。
	 */
	public FilterRegistrationBean() {
	}

	/**
	 * 创建一个新的 {@link FilterRegistrationBean} 实例，并关联指定的 {@link ServletRegistrationBean}。
	 * @param filter 要注册的过滤器
	 * @param servletRegistrationBeans 关联的 {@link ServletRegistrationBean} 数组
	 */
	public FilterRegistrationBean(T filter, ServletRegistrationBean<?>... servletRegistrationBeans) {
		super(servletRegistrationBeans);
		Assert.notNull(filter, "Filter must not be null");
		this.filter = filter;
	}

	@Override
	public T getFilter() {
		return this.filter;
	}

	/**
	 * 设置要注册的过滤器。
	 * @param filter 过滤器实例
	 */
	public void setFilter(T filter) {
		Assert.notNull(filter, "Filter must not be null");
		this.filter = filter;
	}

}
